//
//  MYGFireMessageViewController.swift
//  MYGrow
//
//  Created by 小火科技 on 2024/3/25.
//

import UIKit

class MYGFireMessageViewController: MYGBaseController {
    // MARK: - Property
    let model: MYGChatContentCellModel
    var seeAction: (()->Void)?
    init(model: MYGChatContentCellModel) {
        self.model = model
        
        super.init(nibName: nil, bundle: nil)
        
        modalPresentationStyle = .overFullScreen
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        configUI()
        
        
        if let attribute = model.attribute {
            contentLabel.text = ""
            contentLabel.attributedText = attribute
//            contentLabel.rz.tapAction {[weak self] label, tapActionId, range in
//                self?.model?.labelAction[tapActionId]?()
//            }
        } else {
            if model.content.contains("<img") {
                contentLabel.text = ""
                if let data = UserDefaults.standard.data(forKey: model.content), let attr = try? NSKeyedUnarchiver.unarchivedObject(ofClasses: [NSAttributedString.self], from: data) as? NSAttributedString {
                    self.contentLabel.attributedText = attr
                } else {
                    DispatchQueue.global().async {
                        guard let data = self.model.content.data(using: .utf8), let attr = try? NSAttributedString(
                            data: data,
                            options: [
                                .documentType: NSAttributedString.DocumentType.html,
                                .characterEncoding: String.Encoding.utf8.rawValue
                            ],
                            documentAttributes: nil
                        ) else {
                            return
                        }
                        
                        do {
                            let data = try NSKeyedArchiver.archivedData(withRootObject: attr, requiringSecureCoding: true)
                            UserDefaults.standard.setValue(data, forKey: self.model.content)
                            UserDefaults.standard.synchronize()
                        } catch {
                            print("数据归档失败\(error)")
                        }
                        
                        DispatchQueue.main.async {
                            self.contentLabel.attributedText = attr
//                            self.reloadAction?()
                        }
                    }
                }
            } else {
                contentLabel.attributedText = nil
                contentLabel.text = model.content
            }
        }
        
    }
    // MARK: - UI
    private func configUI() {
        view.backgroundColor = .rgba(0, 0, 0, 0.69)
        view.addSubview(mainView)
        mainView.addSubview(contentLabel)
        
        mainView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.equalTo(205)
        }
        
        contentLabel.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(11)
            make.top.bottom.equalToSuperview().inset(8)
        }
        
    }
    
    // MARK: - lazy load
    private lazy var mainView: UIView = {
        let view = UIView()
        view.backgroundColor = .rgba(247, 247, 247, 1)
        view.layer.masksToBounds = true
        view.layer.cornerRadius = 4
        return view
    }()
    
    private lazy var contentLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(12)
        label.textColor = .rgba(51, 51, 51, 1)
        label.numberOfLines = 0
        return label
    }()
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.seeAction?()
        self.dismiss(animated: false)
    }
}
